﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace LightLibrary {
	public partial class FormScan : Form {
		// === Public =================================================================================================
		public List<FileInfo> Files { get; private set; }

		public FormScan(List<FileInfo> filesForParsing) {
			InitializeComponent();
			scanView.Columns.AddRange(TableTools.GetColsForEditorBooks());
			Files = new List<FileInfo>();
			ParseFiles(filesForParsing);
		}

		// === Private ================================================================================================
		private Dictionary<DataGridViewRow, FileInfo> _fisByRow = new Dictionary<DataGridViewRow, FileInfo>();

		private bool IsDuplicate(FileInfo fiForCheck, FileInfo fiNeighbourd) {
			return fiForCheck.Name == fiNeighbourd.Name && fiForCheck.Extension == fiNeighbourd.Extension && fiForCheck.Length == fiNeighbourd.Length;
		}

		private void ParseFiles(List<FileInfo> filesForParsing) {
			if(filesForParsing.Count == 0) {
				return;
			}
			if(filesForParsing.Count == 1) {
				AddFileToTable(filesForParsing[0], Color.White, true);
				return;
			}
			//check first
			AddFileToTable(filesForParsing[0], IsDuplicate(filesForParsing[0], filesForParsing[1]) ? Color.PaleGreen : Color.White, true);
			// check rest except last!
			for(var i = 1; i < filesForParsing.Count - 1; i++) {
				if(IsDuplicate(filesForParsing[i], filesForParsing[i - 1])) {
					AddFileToTable(filesForParsing[i], Color.Pink, false);
				} else if(IsDuplicate(filesForParsing[i], filesForParsing[i + 1])) {
					AddFileToTable(filesForParsing[i], Color.PaleGreen, true);
				} else {
					AddFileToTable(filesForParsing[i], Color.White, true);
				}
			}
			// check last
			var indexLast = filesForParsing.Count - 1;
			if(IsDuplicate(filesForParsing[indexLast], filesForParsing[indexLast - 1])) {
				AddFileToTable(filesForParsing[indexLast], Color.Pink, false);
			} else {
				AddFileToTable(filesForParsing[indexLast], Color.White, true);
			}
		}

		private void AddFileToTable(FileInfo fi, Color viewColor, bool canCopy) {
			var row = TableTools.CreateRowForBooks(scanView, canCopy, fi.Name, fi.Extension, fi.Length.ToString(), fi.DirectoryName);
			scanView.Rows.Add(row);
			scanView.Rows[scanView.Rows.Count - 1].DefaultCellStyle.BackColor = viewColor;
			_fisByRow.Add(row, fi);
		}

		private void BtnCancelClick(object sender, System.EventArgs e) {
			var dialogResult = MessageBox.Show(@"Are you sure you want to cancel?", @"Are you sure?", MessageBoxButtons.YesNo);
			switch(dialogResult) {
				case DialogResult.Yes:
					Close();
					break;
				default:
					return;
			}
		}

		private void BtnImportClick(object sender, System.EventArgs e) {
			foreach(var row in _fisByRow.Keys) {
				if(bool.Parse(row.Cells["ColumnSelect"].Value.ToString())) {
					Files.Add(_fisByRow[row]);
				}
			}
			Close();
		}
	}
}
